//
//  PrescriptionOrderStatusBottomView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/8/12.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

protocol PrescriptionOrderStatusBottomViewDelegate: NSObjectProtocol {
    func pay(_ view: PrescriptionOrderStatusBottomView)
    func cancel(_ view: PrescriptionOrderStatusBottomView)
    func applyRefund(_ view: PrescriptionOrderStatusBottomView)
    func checkLogistics(_ view: PrescriptionOrderStatusBottomView)
    func confirmReceive(_ view: PrescriptionOrderStatusBottomView)
    func cancelRefund(_ view: PrescriptionOrderStatusBottomView)
    func inputExpressNumber(_ view: PrescriptionOrderStatusBottomView)
}

class PrescriptionOrderStatusBottomView: UIView {
    
    weak var delegate: PrescriptionOrderStatusBottomViewDelegate?
    
    private var viewModel: PrescriptionOrderDetailViewModel?
    
    func bind(to viewModel: PrescriptionOrderDetailViewModel) {
        self.viewModel = viewModel
        
        isHidden = viewModel.bootomViewForDetailHidden
        
        
        setupButton(with: viewModel)
    }
    
    private func setupButton(with viewModel: PrescriptionOrderDetailViewModel) {
        guard let status = viewModel.status else { return }
        
        leftButton.isHidden = viewModel.leftIsHidden
        leftButton.setTitle(status.leftTitle, for: .normal)
        
        rightButton.isHidden = status.rightIsHidden
        rightButton.setTitle(status.rightTitle, for: .normal)
        
        centerButton.isHidden = status.centerIsHidden
        centerButton.setTitle(status.centerTitle, for: .normal)
    }
    
    @IBOutlet private weak var rightButton: UIButton! {
        didSet {
            rightButton.isHidden = true
            rightButton.layer.borderWidth = 1
            rightButton.layer.borderColor = UIColor.mainColor.cgColor
            rightButton.layer.cornerRadius = rightButton.height / 2
            rightButton.setTitleColor(.main, for: .normal)
            rightButton.setBackgroundImage(UIImage(color: .white), for: .normal)
            rightButton.setBackgroundImage(UIImage(color: .background), for: .highlighted)
        }
    }
    
    @IBOutlet private weak var leftButton: UIButton! {
        didSet {
            leftButton.isHidden = true
            leftButton.layer.borderWidth = 1
            leftButton.layer.borderColor = UIColor.textLight.cgColor
            leftButton.layer.cornerRadius = leftButton.height / 2
            leftButton.setTitleColor(.textLight, for: .normal)
            leftButton.setBackgroundImage(UIImage(color: .white), for: .normal)
            leftButton.setBackgroundImage(UIImage(color: .background), for: .highlighted)
        }
    }
    
    @IBOutlet private weak var centerButton: UIButton! {
        didSet {
            centerButton.isHidden = true
            centerButton.layer.borderWidth = 1
            centerButton.layer.borderColor = UIColor.textLight.cgColor
            centerButton.layer.cornerRadius = centerButton.height / 2
            centerButton.setTitleColor(.textLight, for: .normal)
            centerButton.setBackgroundImage(UIImage(color: .white), for: .normal)
            centerButton.setBackgroundImage(UIImage(color: .background), for: .highlighted)
        }
    }
    
    @IBAction private func rightButtonAction(_ sender: UIButton) {
        guard let viewModel = viewModel, let status = viewModel.status else { return }
        
        switch status {
        case .waitPay:              delegate?.pay(self)
        case .shipped:              delegate?.confirmReceive(self)
        case .applyRefund,
             .waitShipApplyRefund,
             .shippedApplyRefund,
             .completeApplyRefund,
             .waitTakeApplyRefund:  delegate?.cancelRefund(self)
        case .waitSendOut,
             .waitSendOutUnreceived,
             .waitSendOutReceived:  delegate?.inputExpressNumber(self)
        default: break
        }
    }
    
    @IBAction private func leftButtonAction(_ sender: UIButton) {
        guard let viewModel = viewModel, let status = viewModel.status else { return }
        
        switch status {
        case .waitPay:      delegate?.cancel(self)
        case .waitShip,
             .shipped,
             .waitTake,
             .completed:    delegate?.applyRefund(self)
        case .waitSendOut,
             .waitSendOutUnreceived,
             .waitSendOutReceived:  delegate?.cancelRefund(self)
        default: break
        }
    }
    
    @IBAction private func centerButtonAction(_ sender: UIButton) {
        delegate?.checkLogistics(self)
    }
}
